
import { UI_Listen, UI_Layer } from '../../config/ui_def';
import scrollOpt from '../base/scrollOptimize'
import { TeamOrder } from '../../config/play_def'
import { Sdk_Platform, WX_AD_CONFIG } from '../../frame/constants';
cc.Class({
    extends: scrollOpt,

    properties: {
        fiveNode: cc.Node,
        toTeam_btn: cc.Node,
        toBag_btn: cc.Node,
        rePlace_btn: cc.Node,
        lvupBtn: cc.Node,
        bottomNode: cc.Node
    },

    initPage() {
        if (!he.pool.getPool('roleitemPre')) {
            he.pool.createPoolSync('roleitemPre', 5, cc.Prefab, 'uimod/roleitemPre')
        }
        if (!he.pool.getPool('roleEquipItem')) {
            he.pool.createPoolSync('roleEquipItem', 5, cc.Prefab, 'uimod/roleEquipItem')
        }

        let addNum = 5 - this.fiveNode.childrenCount
        let reward_prefab = he.load.get("uimod/inFight")
        for (let i = 0; i < addNum; i++) {
            let reward_node = cc.instantiate(reward_prefab)
            this.fiveNode.addChild(reward_node)
        }
    },

    updatePage() {
        this.scrollInit()
        //播放动画使用
        this.itemIndex = 0
        this.aniGrp = []
        this.bigItemInfo = null
        this.smallItemInfo = null
        this.bigItemIndex = -1
        this.smallItemIndex = -1
        this.chooseBig = false
        this.chooseSmall = false   //暂时用不到
        this.inFight = false
        this.gameState = UI_Listen.ROLE_NONE
        gg.eventManager.on('rolePage', this.listenClient, this)
        this.fiveInFight()
        this.creatItemList()
        this.scheduleServer()
        this.openListen()
        this.checkState()
        this.checkBtns()

        this.adSwitch(true)
    },
    closeUI() {
        this.stopSchedule()
        this.closeListen()
        this.clearAni()
        for (let i = (this.content.children.length); i > 0; i--) {
            he.pool.putElement('roleitemPre', this.content.children[i - 1])
        }
    },
    adSwitch(status) {
        if (status) {
            switch (common.sdk_env) {
                case Sdk_Platform.WECHAT:
                    he.ad.interstitialShow()
                    break
                case Sdk_Platform.UC:
                    he.ad.showInterstitialAd()
                    break
            }
        } else {
            //  关掉广告
        }
    },


    colseServer() {
        this.uiBase.closePage()
    },
    //2020.9.23 16：43  老板说没有替换
    listenClient(code) {
        switch (code.type) {
            //切换上面的
            case UI_Listen.ROLE_BIG_CHOOSE:
                this.chooseBigItem(code.info, code.index)
                break
            case UI_Listen.ROLE_SML_CHOOSE:
                this.chooseSmallItem(code.info, code.index)
                break
        }
    },

    clearAni() {
        if (this.aniGrp && this.aniGrp.length != 0) {
            for (let i = 0; i < this.aniGrp.length; i++) {
                this.aniGrp[i].stop()
            }
        }
        this.aniGrp = []

    },

    chooseBigItem(info, index) {
        let isSame = false
        if (this.bigItemIndex == index) {
            isSame = true
        }
        this.bigItemIndex = index
        this.bigItemInfo = info
        for (let i = 0; i < this.content.children.length; i++) {
            let roleItem
            if (i % 2 == 0) {
                roleItem = this.content.children[i].getChildByName('roleItem').getComponent('roleItem')
            } else {
                roleItem = this.content.children[i].getChildByName('roleItem2').getComponent('roleItem')
            }
            roleItem.hideLight()
        }
        if (!isSame) {
            let chooItem
            if (index % 2 == 0) {
                chooItem = this.content.children[index].getChildByName('roleItem').getComponent('roleItem')
            } else {
                chooItem = this.content.children[index].getChildByName('roleItem2').getComponent('roleItem')
            }
            chooItem.showLight()
            this.chooseBig = true
        } else {
            this.bigItemIndex = -1
            this.chooseBig = false
        }
        // console.log(this.bigItemInfo)
        this.inFight = this.bigItemInfo.order >= 1 ? true : false
        this.isInTeam = this.checkInTeam(this.bigItemInfo.id)
        // console.log(this.isInTeam)
        // console.log(this.inFight)
        // console.log(isSame)
        this.checkState()
        this.checkBtns()
        if (this.inFight && !isSame && this.isInTeam.isHave) {
            this.chooseFightItem(this.bigItemInfo.order)
        } else if (!this.inFight && !isSame && this.isInTeam.isHave) {
            this.toTeam_btn.active = true
            this.hideAllFightItem()
        }
        else {
            this.toTeam_btn.active = false
            this.hideAllFightItem()
        }

    },

    checkInTeam(num) {
        let teamGrp = he.local.getJson('team', 'user')
        for (let i = 0; i < teamGrp.length; i++) {
            if (teamGrp[i].id == num) {
                return true
            }
        }
        return false
    },

    chooseFightItem(order) {
        let index
        for (let i = 0; i < TeamOrder.length; i++) {
            if (TeamOrder[i] == order) {
                index = i
            }
        }
        this.hideAllFightItem()
        this.fiveNode.children[index].getComponent('roleInFight').showLight()
    },

    hideAllFightItem() {
        for (let j = 0; j < this.fiveNode.children.length; j++) {
            this.fiveNode.children[j].getComponent('roleInFight').hideLight()
        }
    },

    chooseSmallItem(info, index) {
        // let isSame = false
        // if (this.smallItemIndex == index) {
        //     isSame = true
        // }
        // this.smallItemIndex = index
        // this.smallItemInfo = info
        // for (let i = 0; i < this.fiveNode.children.length; i++) {
        //     let roleItem
        //     roleItem = this.fiveNode.children[i].getComponent('roleInFight')
        //     roleItem.hideLight()
        // }
        // if (!isSame) {
        //     let chooItem
        //     chooItem = this.fiveNode.children[index].getComponent('roleInFight')
        //     chooItem.showLight()
        //     this.chooseSmall = true
        // } else {
        //     this.smallItemInfo = -1
        //     this.chooseSmall = false
        // }
        // this.checkState()
        // this.checkBtns()
        this.bigItemInfo = info
        this.smallItemIndex = index
        this.smallItemInfo = info
        this.chooseFightItem(this.smallItemInfo.order)
        let bigIndex = this.smallItemInfo.id - 1001
        for (let i = 0; i < this.content.children.length; i++) {
            let roleItem
            if (i % 2 == 0) {
                roleItem = this.content.children[i].getChildByName('roleItem').getComponent('roleItem')
            } else {
                roleItem = this.content.children[i].getChildByName('roleItem2').getComponent('roleItem')
            }
            roleItem.hideLight()
        }
        let chooItem
        if (bigIndex % 2 == 0) {
            chooItem = this.content.children[bigIndex].getChildByName('roleItem').getComponent('roleItem')
        } else {
            chooItem = this.content.children[bigIndex].getChildByName('roleItem2').getComponent('roleItem')
        }
        chooItem.showLight()
        this.gameState = UI_Listen.ROLE_GOHOME
        this.checkBtns()
    },

    creatItemList() {
        //暂时写死
        let roleInfo = [1001, 1002, 1003, 1004, 1005]
        if (roleInfo && roleInfo.length > 0) {
            for (let i = 0; i < roleInfo.length; i++) {
                let pre = he.pool.getElement('roleitemPre')
                pre.parent = this.content
                let info = this.checkInTeam(roleInfo[i])
                let roleItem
                if (i % 2 == 0) {
                    roleItem = pre.getChildByName('roleItem')
                    pre.x += 800
                } else {
                    roleItem = pre.getChildByName('roleItem2')
                    pre.x -= 800
                }
                roleItem.active = true
                let func = roleItem.getComponent('roleItem')
                func.init(info, i)
                if (i == 0) {
                    func.clickServer()
                }
            }
        }
        this.scroll.scrollToTop()
    },

    checkInTeam(id) {
        let info = {}
        let team = he.local.getJson('team', 'user')
        let isHave = false
        let msg = { id: id }
        for (let i = 0; i < team.length; i++) {
            if (id == team[i].id) {
                isHave = true
                msg = team[i]
                break
            }
        }
        info.isHave = isHave
        info.msg = msg
        return info
    },
    //////////////////////////////////////////进场动画、、、、、、、、、、、、、、、、、

    scheduleServer() {
        this.schedule(this.aniServer, 0.1, this.content.children.length, 0)
    },

    stopSchedule() {
        this.unschedule(this.aniServer)
    },

    aniServer() {
        if (!this.content.children) return
        if (this.itemIndex >= this.content.children.length) {
            this.stopSchedule()
            return
        }
        this.aniGrp[this.itemIndex] = cc.tween(this.content.children[this.itemIndex])
            .to(0.3, { x: 0 })
            .start()
        this.itemIndex += 1
    },
    //////////////////////////////////////五个出战框
    fiveInFight() {
        let team = he.local.getJson('team', 'user')
        let grp = []
        if (team && team.length != 0) {
            for (let i = 0; i < team.length; i++) {
                let item
                if (team[i].order != -1) {
                    let realIndex = this.getIndex(team[i].order)
                    grp.push(realIndex)
                    let script = this.fiveNode.children[realIndex].getComponent('roleInFight')
                    script.init(team[i], realIndex)
                }
            }

            let numGrp = [0, 1, 2, 3, 4]
            let diff = this.getArrDifference(grp, numGrp)
            for (let j = 0; j < diff.length; j++) {
                let script = this.fiveNode.children[diff[j]].getComponent('roleInFight')
                script.init(null, diff[j])
            }
        }
    },
    getArrDifference(arr1, arr2) {
        return arr1.concat(arr2).filter(function (v, i, arr) {
            return arr.indexOf(v) === arr.lastIndexOf(v);
        });
    },
    getIndex(num) {
        for (let i = 0; i < TeamOrder.length; i++) {
            if (TeamOrder[i] == num) {
                return i
            }
        }
    },
    ///////////////////////////////////////阶段判断、、、、、、、、、、、、、、、、、、、、、、、、
    checkState() {
        // if (this.chooseBig && this.chooseSmall) {
        //     this.gameState = UI_Listen.ROLE_BOTH
        // } else if (this.chooseBig && !this.chooseSmall) {
        //     this.gameState = UI_Listen.ROLE_OL_BIG
        // } else if (!this.chooseBig && this.chooseSmall) {
        //     this.gameState = UI_Listen.ROLE_OL_SMALL
        // } else if (!this.chooseBig && !this.chooseSmall) {
        //     this.gameState = UI_Listen.ROLE_NONE
        // }

        if (this.chooseBig && this.inFight) {
            this.gameState = UI_Listen.ROLE_GOHOME
        } else if (this.chooseBig && !this.inFight) {
            this.gameState = UI_Listen.ROLE_GOFIGHT
        } else if (!this.chooseBig) {
            this.gameState = UI_Listen.ROLE_NONE
        }
    },

    checkBtns() {
        switch (this.gameState) {
            // case UI_Listen.ROLE_BOTH:
            //     this.toBag_btn.active = false
            //     this.toTeam_btn.active = false
            //     this.rePlace_btn.active = false
            //     this.lvupBtn.active = true
            //     break
            // case UI_Listen.ROLE_OL_BIG:
            //     this.toBag_btn.active = false
            //     this.toTeam_btn.active = true
            //     this.rePlace_btn.active = false
            //     this.lvupBtn.active = true
            //     break
            // case UI_Listen.ROLE_OL_SMALL:
            //     this.toBag_btn.active = true
            //     this.toTeam_btn.active = false
            //     this.rePlace_btn.active = false
            //     this.lvupBtn.active = false
            //     break
            case UI_Listen.ROLE_NONE:
                this.toBag_btn.active = false
                this.toTeam_btn.active = false
                this.rePlace_btn.active = false
                this.lvupBtn.active = false
                break
            case UI_Listen.ROLE_GOHOME:
                this.toBag_btn.active = true
                this.toTeam_btn.active = false
                this.rePlace_btn.active = false
                this.lvupBtn.active = true
                break
            case UI_Listen.ROLE_GOFIGHT:
                this.toBag_btn.active = false
                this.toTeam_btn.active = true
                this.rePlace_btn.active = false
                this.lvupBtn.active = true
                break
        }
    },
    //上场server
    goFightServer() {
        if (!this.bigItemInfo) return
        let team = he.local.getJson('team', 'user')
        let teamGrp = []
        let oriGrp = [1, 2, 3, 4, 5]
        for (let i = 0; i < team.length; i++) {
            if (team[i].order >= 1) {
                teamGrp.push(team[i].order)
            }
        }
        let diffGrp = this.getArrDifference(teamGrp, oriGrp)
        // console.log(diffGrp)
        let newIndex = -1
        if (diffGrp && diffGrp.length > 0) {
            for (let j = 0; j < TeamOrder.length; j++) {
                if (TeamOrder[j] == diffGrp[0]) {
                    newIndex = j
                    break
                }
            }
        } else {
            gg.uiManager.addTip('出战队伍已满')
            return
        }
        for (let j = 0; j < team.length; j++) {
            if (team[j].id == this.bigItemInfo.id) {
                team[j].order = TeamOrder[newIndex]
                this.bigItemInfo = team[j]
                break
            }
        }

        he.local.setJson('team', team, 'user')
        this.chooseBig = false
        this.checkState()
        this.checkBtns()
        this.updateBigInfo(this.bigItemInfo)
        this.fiveInFight()
        // this.teamInit()
        // this.updateBag()
    },

    updateBigInfo(info) {
        if (this.content) {
            let index = info.id - 1001
            let chooItem
            if (index % 2 == 0) {
                chooItem = this.content.children[index].getChildByName('roleItem').getComponent('roleItem')
            } else {
                chooItem = this.content.children[index].getChildByName('roleItem2').getComponent('roleItem')
            }
            chooItem.init({ msg: info, isHave: true }, index)
        }
    },
    //撤下
    removeServer() {
        if (!this.bigItemInfo) return
        let team = he.local.getJson('team', 'user')
        this.bigItemIndex = -1
        this.bigItemInfo.order = -1
        for (let i = 0; i < team.length; i++) {
            if (team[i].id == this.bigItemInfo.id) {
                team[i].order = -1
                break
            }
        }
        he.local.setJson('team', team, 'user')
        this.chooseBig = false
        this.checkState()
        this.checkBtns()
        this.updateBigInfo(this.bigItemInfo)
        this.fiveInFight()
    },

    //详情
    detailServer() {
        this.uiBase.closePage()

        gg.uiManager.addPage(UI_Layer.Cover, {
            name: 'equipmentPage',
            cover: true,
            // close: true,
            index: (this.bigItemInfo.id - 1001)
        })
    },





});
